//随机出现
function randomNum(m, n) {
	return Math.floor(Math.random() * (n - m)) + m;
}
class DiPlane extends Base {
	constructor(obj, myPlane) {
		super(obj)
		//得到我方战机，以便计算
		this.myPlane = myPlane;
		//初始化
		this.init()
	}
	init() {
		this.bodyWidth = document.body.clientWidth;
		this.bodyHeight = document.body.clientHeight;
		//生成随机出现的位置
		this.top = -20;
		this.left = randomNum(0, this.bodyWidth);
		$(this.dom).css('left', this.left + 'px')
		$(this.dom).css('top', this.top + 'px')
		//每隔固定的时间设置速度
		this.setSpeed();
		this.speedInter = setInterval(_ => {
			this.setSpeed()
		}, 3000)
		//开始移动
		this.move()
	}
	//移动 上下左右均可
	move() {
		this.moveInter = setInterval(_ => {
			this.left += this.speedX;
			this.top += this.speedY;
			//边界判断
			if (this.left > this.bodyWidth + 50) {
				this.remove()
			} else if (this.left < -100) {
				this.remove()
			}
			if (this.top > this.bodyHeight) {
				this.remove()
			} else if (this.top < -100) {
				this.remove()
			}

			$(this.dom).css('left', this.left + 'px')
			$(this.dom).css('top', this.top + 'px')
			//没移动一次，就检查和我方战机的碰撞
			this.checkBeng(this, this.myPlane)
		}, 40)
	}
	setSpeed() {
		this.speedX = randomNum(...this.imgs.speedX);
		this.speedY = randomNum(...this.imgs.speedY);
	}
	remove(isPeng) {
		this.dom.remove()
		clearInterval(this.moveInter)
		clearInterval(this.speedInter)
		if (isPeng) {
			//更新击毁敌机数量
			const jDom = $('#box>p:nth-child(1)>span');
			//获取旧的积分并更新到新的分数
			const num = Number(jDom.text()) + 1;
			//设置新的值
			jDom.text(num)
		}
	}
}
